<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use app\common\model\Prt;
use app\common\controller\AgentBaseController;

class Store extends AgentBaseController
{
	//我的房台	
	public function house_manage(){
		if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['chain_id']))$where['a.chain_id'] = ['like','%'.$param['chain_id'].'%'];
			if(!empty($param['type_id']))$where['a.type_id'] = ['like','%'.$param['type_id'].'%'];
    	}		
		$where['a.agent_id'] = session('agent_id');
		$where['a.state'] = 1;
		$list=db("agent_house")->alias('a')
		->join('agent_store b','a.chain_id = b.id')
		->join('agent_housetype c','a.type_id = c.id')
		->field('a.id,a.chain_id,a.name,a.type_id,a.scope_limit,a.status,b.store_name,c.type_name')
		->where($where)->paginate(20);
		$this->assign('list',$list);
		$storelist=db('agent_store')->where('agent_id',session('agent_id'))->select();
		$this->assign('storelist',$storelist);
		$typelist=db('agent_housetype')->where('agent_id',session('agent_id'))->select();
		$this->assign('typelist',$typelist);
		return $this->fetch();
	}
	//添加房台
	public function house_add()
    {
    	if(request()->isPost()){
    		$storeinfo=db('agent_store')->where('id',input("chain_id"))->find();
			
			$param['name'] 	= input("name");
			$param['status'] 	= input("status");
			$param['scope_limit'] 	= input("scope_limit");
			$param['chain_id'] 	= input("chain_id");
			$param['agent_id'] 	= session("agent_id");
			$param['type_id'] 	= input("type_id");
			$param['add_time'] 	= now_time();;
			$res = db('agent_house')->insert($param);
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
    	}
		$storelist=db('agent_store')->where('agent_id',session('agent_id'))->where('state',1)->select();
		$this->assign('storelist',$storelist);
		$typelist=db('agent_housetype')->where('agent_id',session('agent_id'))->select();
		$this->assign('typelist',$typelist);
    	return $this->fetch();
    }
	//编辑房台
	public function house_edit()
    {
    	if(request()->isPost()){
			$param['name'] 	= input("name");
			$param['scope_limit'] 	= input("scope_limit");
			$param['status'] 	= input("status");
			$param['type_id'] 	= input("type_id");
			$param['chain_id'] 	= input("chain_id");
			$res = db('agent_house')->where('id',input("id"))->update($param);
			if($res){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
		$storelist=db('agent_store')->where('agent_id',session('agent_id'))->select();
		$this->assign('storelist',$storelist);
		$typelist=db('agent_housetype')->where('agent_id',session('agent_id'))->select();
		$this->assign('typelist',$typelist);
		$data=db('agent_house')->where('id',input("id"))->find();
		$this->assign('data',$data);
    	return $this->fetch();
    }
	//删除房台
	public function house_del(){
		if(request()->isAjax()){
			//修改房台信息
			$res = db('agent_house')->where('id',input('id'))->update(['state' => 2]);
			if($res){
				return json(['code'=>200,'msg'=>'删除成功']);
			}else{
				return json(['code'=>402,'msg'=>'删除失败']);
			}
    	}
	}
	//房台分类
	public function house_type()
    {
    	$agentid = session('agent_id');
    	$list = Db::table('agent_housetype')->where('agent_id',$agentid)->order('id desc')->select();
        $this->assign('list',$list);
    	return $this->fetch();
    }	
	//房台分类添加
    public function house_type_add()
    {
    	$agentid = session('agent_id');
    	if($_POST){
            $data['type_name']=$_POST['type_name'];
            $data['agent_id']=$agentid;
            $result=Db::table('agent_housetype')->insert($data);
            if($result){
			return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	
    	return $this->fetch();
    }
	//房台分类修改
    public function house_type_update()
    {
    	$id = input('id');
    	if($_POST){
    		$data['type_name']=$_POST['type_name'];
            $result=Db::table('agent_housetype')->where('id',$id)->update($data);;
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
        $list = Db::table('agent_housetype')->where('id',$id)->find();
        $this->assign('list',$list);
    	return $this->fetch();
    }
	//房台分类删除
    public function house_type_del()
    {
    	$id = input("id");
    	$res=Db::table('agent_house')->where('type_id',$id)->find();
    	if($res){
			return json(['code'=>408,'msg'=>'该类有房台，不能删除']);
        }else{
       		$result = Db::table('agent_housetype')->where('id',$id)->delete();
       		if($result){
			return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }     	
    }
	//我的门店
	public function store_manage()
    {
    	if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['store_name']))$where['m.store_name'] = ['like','%'.$param['store_name'].'%'];
			if(!empty($param['addres']))$where['m.addres'] = ['like','%'.$param['addres'].'%'];
    	}
    	$where['m.agent_id'] = session('agent_id');
    	$where['m.state'] = ['<>',2];
    	
    	$field = 'm.*,b.count';
    	$clerknum = Db::table('agent_clerk')->field('count(id) count,chain_id')->where('state','1')->group('chain_id')->buildSql();
    	$list = Db::name("agent_store")->alias('m')->
		join($clerknum.' b' ,'b.chain_id = m.id','LEFT')->
		field($field)->where($where)->order('m.add_time desc')->select();
		$this->assign("list",$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    public function store_add()
    {
		if(request()->isPost()){
			$param['add_time'] 	= now_time();
			$param['store_name'] 	= input("store_name");
			$param['addres'] 	= input("addres");
			$param['agent_id'] 	= session("agent_id");
			$res = db('agent_store')->insert($param);
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
    	}
    	return $this->fetch();
    }
	public function store_edit(){
		if(request()->isPost()){
			$param['store_name'] 	= input("store_name");
			$param['addres'] 	= input("addres");
			$param['id']=input("id");
			$res = db('agent_store')->update($param);
			if($res){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
		$account=db('agent_store')->find(input("id"));
		$this->assign('ad',$account);
		return $this->fetch();
	}
	
	//删除门店
	public function store_del(){
		if(request()->isAjax()){
			//获取门店信息
			$res = db('agent_store')->where('id',input('id'))->update(['state' => 2]);
			if($res){
				return json(['code'=>200,'msg'=>'删除成功']);
			}else{
				return json(['code'=>402,'msg'=>'删除失败']);
			}
    	}
	}
	
	//门店打印机设置
	public function daying_edit(){
		$mid=input('mid');
		$this->assign('mid',$mid);
		$account=db('agent_prt')->where('chain_id',$mid)->find();
		$this->assign('ad',$account);
		if(request()->isPost()){
			$param['machine_code'] 	= input("machine_code");
			$param['msign'] 	= input("msign");
			//$param['partner'] 	= input("partner");
			//$param['apiKey'] 	= input("apiKey");
			//$param['ptname'] 	= input("ptname");
			//$param['password'] 	= input("password");
			if($account){
				$res = db('agent_prt')->where('id',input("id"))->update($param);
			}else{
				$param['chain_id'] 	= $mid;
				$param['agent_id'] 	= session("agent_id");
				$res = db('agent_prt')->insert($param);
				if($res){
					$prt=new Prt();	
					$prt->action_addprint('',input("machine_code"),'',session("agent_id"),'','',input("msign"));
				}
			}
			if($res){
				return json(['code'=>200,'msg'=>'设置成功']);
			}else{
				return json(['code'=>402,'msg'=>'设置失败']);
			}
    	}
		
		return $this->fetch();
	}
    //收银员
	public function cashier_manage()
    {
    	$typelist=db('agent_store')->where('agent_id',session('agent_id'))->select();
		$this->assign('typelist',$typelist);
		
		
		if(request()->isPost()){
    		$param=input('post.');
			if(!empty($param['namea']))$where['a.name'] = ['like','%'.$param['namea'].'%'];
			if(!empty($param['chain_id']))$where['a.chain_id'] = ['like','%'.$param['chain_id'].'%'];
    	}
    	$where['a.agent_id'] = session('agent_id');
    	$where['a.state'] = ['<>',3];
		$data=Db::table('agent_clerk')
    	->alias('a')
    	->join('agent_store w','a.chain_id = w.id')
    	->field('a.*,w.store_name store_name')
    	->where($where)
    	->order('a.add_time desc')
		->paginate(20);
		$this->assign('data',$data);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }
    
    //删除收银员
    function cashier_del(){
    	$cashier_id = input('id');
    	
    	//修改收银员状态
    	$resut = db('agent_clerk')->where('id', $cashier_id)->update(['state' => 3]);
    	
    	if($resut){
    		return json(['code'=>200,'msg'=>'删除成功']);
    	}else{
    		return json(['code'=>402,'msg'=>'删除失败']);
    	}
    	
    }
    
	//添加收银员
    public function cashier_add()
    {
    	if(request()->isPost()){
    		//判断是否已是收银员
    		$clerk_info = db('agent_clerk')->where('mobile',input("mobile"))->where('state','<>','3')->find();
			if($clerk_info){
				return json(['code'=>405,'msg'=>'当前用户已是收银员']);
			}
			
    		//获取用户信息，openid不为空
    		$user_info = db('info_user')->where('id',input("no"))->where('openid','<>','')->find();
    		if($user_info){
    			$storeinfo=db('agent_store')->where('id',input("chain_id"))->find();
				$param['no'] 			= input("no");
				$param['mobile'] 		= input("mobile");
				$param['name'] 			= $user_info["nickname"];
				$param['logo'] 			= input("logo");
				$param['password'] 		= $user_info['password'];
				$param['state'] 		= input("state");
				$param['ctype'] 		= input("ctype");
				$param['chain_id'] 		= input("chain_id");
				$param['agent_id'] 		= session("agent_id");
				$param['oem_id'] 		= session("oem_id");
				$param['add_time'] 		= now_time();
				$param['chain_name'] 	= $storeinfo['store_name'];
				$param['user_id'] 		= $user_info['id'];
				$res = db('agent_clerk')->insert($param);
				if($res){
					return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
				}else{
					return json(['code'=>402,'msg'=>'添加失败']);
				}
    		}else{
    			return json(['code'=>405,'msg'=>'当前用户不是会员']);
    		}
    	}
    	$typelist=db('agent_store')
					->where('agent_id',session('agent_id'))
					->where('state',1)
					->select();
		$this->assign('typelist',$typelist);
    	return $this->fetch();
    }
    public function cashier_edit()
    {
    	if(request()->isPost()){
			$param['no'] 	= input("no");
			$param['mobile'] 	= input("mobile");
			$param['name'] 	= input("name");
			$param['logo'] 	= input("logo");
			if(input("password")){
				$param['password'] 	= md5_salt(input("password"));
			}
			$param['state'] 	= input("state");
			$param['ctype'] 	= input("ctype");
			$param['chain_id'] 	= input("chain_id");
			$res = db('agent_clerk')->where('id',input("id"))->update($param);
			if($res){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
    	}
    	$typelist=db('agent_store')->where('agent_id',session('agent_id'))->where('state',1)->select();
		$this->assign('typelist',$typelist);
		$data=db('agent_clerk')->where('id',input("id"))->find();
		$this->assign('data',$data);
    	return $this->fetch();
    }
    
    //门店收款二维码
	public function fukuan()
    {
    	$agent_info = db('info_agent')->where('id',session('agent_id'))->find();
    	$agent_store = db('agent_store')->where('id',input('store_id'))->find();
    	
    	$code_url="http://".$_SERVER['HTTP_HOST']."/index/pay/qrcode_pay?agent_id=".$agent_info['id']."&store_id=".$agent_store['id']."&clerk_id=0";
    	
    	$agent_info['code_url'] = $code_url;
    	$agent_info['agent_store_name'] = $agent_store['store_name'];
    	
    	$this->assign('agent_info',$agent_info);
    	
    	
    	return $this->fetch();
    }
    //收银员收款二维码
	public function fukuan_s()
    {
    	$agent_clerk = db('agent_clerk')->where('id',input('clerk_id'))->find();
    	$agent_store = db('info_agent')->where('id',input('agent_id'))->find();
    	$code_url="http://".$_SERVER['HTTP_HOST']."/index/pay/qrcode_pay?agent_id=".$agent_clerk['agent_id']."&store_id=".$agent_clerk['chain_id']."&clerk_id=".$agent_clerk['id'];
    	
    	$agent_info['code_url'] = $code_url;
    	$agent_info['agent_store_name'] = $agent_store['store_name'];
    	$agent_info['agent_name'] = $agent_clerk['mobile'];
    	$agent_info['logo'] = $agent_clerk['logo'];
    	
    	$this->assign('agent_info',$agent_info);
    	
    	
    	return $this->fetch();
    }
    
    
    
}
